home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1061 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  55 lines

  1. Path: nntp.teleport.com!muse
  2. From: muse@teleport.com (Andrew T Millard)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Random numbers...?
  5. Date: 11 Jan 1996 06:54:16 GMT
  6. Organization: Teleport - Portland's Public Access (503) 220-1016
  7. Message-ID: <4d2c6o$h7l@maureen.teleport.com>
  8. References: <30DDBEB1.4C42@aruba.ccit.arizona.edu> <DKDz1v.736@eskimo.com>
  9. NNTP-Posting-Host: kelly.teleport.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. mAg (mag@eskimo.com) wrote:
  13. : In article <30DDBEB1.4C42@aruba.ccit.arizona.edu> (Sun, 24 Dec 1995 13:57:21 -0700), 
  14. : vaughn@aruba.ccit.arizona.edu says :
  15. : >
  16. : >Hello,
  17. : >Thanks to those who helped me with my last question on 
  18. : >cache-flushing. I realize I'm showing my newness to C by even asking 
  19. : >this, but I need to get some code that will simply generate a random 
  20. : >integer from 0 to 9. Specifically, I want to generate a string of 
  21. : >four random digits. I keep generating numbers that look suspiciously 
  22. : >non-random somehow (seeding the random number generator with the 
  23. : >current time). Any ideas?
  24. : >
  25. : >Thanks,
  26. : >Bill Vaughn
  27.  
  28. Bill, this routine should do fine:
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32.  
  33. void main(void)
  34. {
  35.     int iCount, digit[4];
  36.  
  37.     srand(time());   /*  Seeds random numbers  */    
  38.     for(iCount = 0; iCount <= 3; iCount++)
  39.     {
  40.         digit[iCount] = abs(rand() % 10);
  41.     }
  42. }
  43.  
  44.     If this doesn't work, then the Gods are just angry at you.  :)
  45.                                       Newbie-man Bjornus
  46.  
  47.  
  48. --
  49. "There is a man, I think, who might have what you need."
  50.                         Mnemo
  51.  
  52. "Heavy hung the canopy of blue..."
  53.                             Green is the Color
  54.                             Pink Floyd
  55.